﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


public partial class kecheng_confirm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        if (e.NewSelectedIndex >= 0)
        {
            //sqldsEmpDetails.SelectCommand = "SELECT LastName,FirstName,Title,BirthDate,Address, City, Country, HomePhone FROM Employees where EmployeeID=@paramEmployeeID";
            SqlDataSource2.SelectCommand = "SELECT name,writer,laoshi,jigou,jianjie from kecheng where id=@paramid";
            if (SqlDataSource2.SelectParameters.Count <= 0)
            {
                SqlDataSource2.SelectParameters.Add("paramid", TypeCode.Int16, "");
            }

            SqlDataSource2.SelectParameters["paramid"].DefaultValue = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;

            DetailsView1.DataSourceID = SqlDataSource2.ID;
            DetailsView1.Visible = true;
        }
        else
        {
            DetailsView1.Visible = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int j = 0;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                j = j + 1;
                string zid = GridView1.Rows[i].Cells[3].Text;         //e.Row.Cells[0].Text
                if (this.ExeSql(zid) == 1)
                {

                    Label2.Text = "审核成功！";

                    //对以下代码的说明：审核课程成功以后添加相应名称的论坛
                    Conn conn = new Conn();
                    string cname = conn.sqlFirst("select [name] from kecheng where [id]="+zid);
                    string sqlbbsid = "select max(bbs_id)+1 from bbsconfig where forum_no=12";
                    string bbsid = conn.sqlFirst(sqlbbsid);
                    string sqlbbs = "insert into bbsconfig(rightlist,bbsname,urlpage) values('" + zid + "','" + cname + "','../bbs/content.aspx')";
                    if (conn.ExeSql(sqlbbs) == 1)
                    {
                        Label2.Text = Label2.Text + " 对应论坛添加成功！";
                        //反写kecheng表的topic字段为论坛编号
                        string forumid = conn.sqlFirst("select bbs_id from bbsconfig where rightlist='"+zid+"' and bbsname='"+cname+"'");
                        if (conn.ExeSql("update kecheng set topic='"+forumid+"' where [id]="+zid) == 1)
                        {
                            Label2.Text = Label2.Text + " 课程和论坛匹配成功！";
                            //生成子栏目
                            conn.ExeSql("insert into subbbs(bbs_id,subbbs_name) values(" + bbsid + ",'教程')");
                            conn.ExeSql("insert into subbbs(bbs_id,subbbs_name) values(" + bbsid + ",'FAQ')");
                            conn.ExeSql("insert into subbbs(bbs_id,subbbs_name) values(" + bbsid + ",'答疑室')");
                            conn.ExeSql("insert into subbbs(bbs_id,subbbs_name) values(" + bbsid + ",'课程讨论')");
                        }
                        else
                        {
                            Label2.Text = Label2.Text + " 课程和论坛匹配失败！";
                        }
                    }
                    else
                    {
                        Label2.Text = Label2.Text + " 对应论坛添加失败！";
                    }

                }
                else
                {
                    Label2.Text = "审核失败！";
                }

            }

        }
        if (j == 0)
        {
            Label2.Text = "未选择课程。";
        }
        else
        {
            GridView1.DataBind();
        }
    }
    public int ExeSql(string zid)   //审核通过
    {
        DateTime dxtsj2 = DateTime.Now;

        //string Sql1 = "update kecheng set [xtsj2]='" + dxtsj2 + "',[confirm]='1') where id=convert(int,'" + zid + "')";
        string Sql1 = "update kecheng set [xtsj2]='" + dxtsj2 + "',[confirm]='1' where id='" + zid + "'";
        
        string myConnectionString = ConfigurationSettings.AppSettings["gConnectionString"];
        SqlConnection myConnection = new SqlConnection(myConnectionString);
        SqlCommand myCommand1 = new SqlCommand(Sql1, myConnection);
        
        try
        {
            myConnection.Open();
            myCommand1.ExecuteNonQuery();
            myCommand1.Dispose();
            myConnection.Close();
            return 1;
        }
        catch
        {
            myConnection.Close();
            return 0;
        }
    }
}
